/**
 * 抽象出来的FileItem对象,包含File和Dir.
 */
import { FileManager } from './FileManager'


export class FileItem {
  path?: string
  name?: string
  // size:number
  isExisted: boolean = false;
  isDir: boolean = false
  //创建时的UTC时间戳in ms
  createTime?: number;

  constructor(filePath: string) {
    this.path = filePath;
    let arr = filePath.split("/");
    this.name = arr[arr.length-1];
    //获取文件是否存在
    this.isExisted = FileManager.isItemExist(this.path);
    if (this.isExisted) {
      let stat = FileManager.getFileInfo(this.path);
      this.createTime = stat.ctime;
      this.isDir = !stat.isFile();
    }
  }

  getSize(): number {
    if (this.path) {
      let stat = FileManager.getFileInfo(this.path);
      let size = stat.size;
      return size;
    }
    else {
      return 0;
    }
  }

  //删除
  remove() {
    this.isExisted = false;
    if (this.isDir) {
      //删除子项目.
      FileManager.removeDirR(this.path!);
    }
    else {
      FileManager.removeFile(this.path!);
    }
  }

  //返回当前FileItem所在文件夹
  getDirPath(): string {
    let arr = this.path!.split("/");
    arr.splice(arr.length - 1, 1);
    let dirPath = arr.join("/");
    return dirPath;
  }

  //
  // }
  //
  // + (NSMutableArray *)fileListInDirPath:(NSString *)dirPath {
  // 	NSMutableArray <JsFile *>* marr = [NSMutableArray array];
  //
  // 	NSFileManager * fileManger = [NSFileManager defaultManager];
  // 	NSArray * dirArray = [fileManger contentsOfDirectoryAtPath:dirPath error:nil];
  // 	NSString * filePath = nil;
  // 	for (NSString * fileName in dirArray) {
  // 		filePath  = [dirPath stringByAppendingPathComponent:fileName];
  // 		BOOL isDir = NO;
  // 		[fileManger fileExistsAtPath:filePath isDirectory:&isDir];
  // 		if(!isDir) {
  // 			JsFile * file = [JsFile fileWithFilePath:filePath];
  // 			[marr addObject:file];
  // 		}
  // }
  // //排序,字典排序小的在前,大的在后,Ascending.
  // //这里取反,大的在前.
  // [marr sortUsingComparator:^NSComparisonResult(JsFile *  _Nonnull obj1, JsFile *  _Nonnull obj2) {
  // 		NSComparisonResult result = [obj1.fileName compare:obj2.fileName];
  // 		if(result == NSOrderedAscending) {
  // 				//日期小的放后面.
  // 			return NSOrderedDescending;
  // 		}
  // else {
  //   return NSOrderedAscending;
  // }
  // }];
  // return marr;
  // }


}



